<template>
	<view class="shareBox">
		<view class="shareLayer" @click.stop="previewClick">
			<view class="shareCon">
				<image v-if="shareImg" class="poster" :src="shareImg" mode="widthFix"></image>
				<!-- <uni-icons type="close" size="30"></uni-icons> -->
			</view>
		</view>
		<uni-mdshare ref="shareRef" :showMoreOper="false" @shareClick="shareClick"></uni-mdshare>
	</view>
</template>

<script>
	import { inviteShare } from "@/api/index.js"
	import { requestAndroidPermission,previewImg } from "@/utils/common.js"
	import uniMdshare from "@/pages/school/components/uni-mdshare/uni-mdshare.vue"
	export default {
		components:{
			uniMdshare
		},
		data() {
			return {
				optionsQuery:'',
				shareImg:''
			};
		},
		onLoad(option) {
			// optionsQuery.inviteType 1提问者邀请；2最佳回答者邀请；3普通用户的提问邀请；4邀请加入小组；5任务打卡邀请，6、接力找人
			this.optionsQuery=option.query?JSON.parse(option.query):''
			console.log('参数',this.optionsQuery)
			this.getShareParam()
			this.$nextTick(()=>{
				this.$refs.shareRef.show()
			})
		},
		methods:{
			getShareParam(){
				uni.showLoading({
					title:'海报生成中',
					mask:true
				})
				let userId=uni.getStorageSync('userId') || ''
				let param={}
				// 唤醒app的h5下载页
				let domainUrl="https://file.alhelp.net/kwkd-appdownload/index.html"//以前是 https://info.alhelp.net/kwkd-appdownload/index.html
				if(this.optionsQuery.inviteType<=3){
					// 识别二维码后都是跳转到提问详情
					let query={
						id:this.optionsQuery.id,
						questionId:this.optionsQuery.questionId
					}
					param={
						id:this.optionsQuery.questionId>0?this.optionsQuery.questionId:this.optionsQuery.id,
						inviteType:this.optionsQuery.inviteType,
						url:`${domainUrl}?url=/pages/send/question/questionDetail/questionDetail?item=${JSON.stringify(query)}&userId=${userId}`
					}
				}else if(this.optionsQuery.inviteType==4){
					// 邀请加入小组
					param={
						id:this.optionsQuery.id,
						inviteType:this.optionsQuery.inviteType,
						url:`${domainUrl}?url=/pages/send/group/groupDetail/groupDetail?id=${this.optionsQuery.id}&userId=${userId}`
					}
				}else if(this.optionsQuery.inviteType==5){
					// 任务打卡
					param={
						id:this.optionsQuery.id,
						inviteType:this.optionsQuery.inviteType,
						url:`${domainUrl}?url=/pages/send/task/taskDetail/taskDetail?id=${this.optionsQuery.id}&groupId=${this.optionsQuery.groupId}&userId=${userId}`
					}
				}else if(this.optionsQuery.inviteType==6){
					//接力找人
					param={
						id:this.optionsQuery.id,
						inviteType:this.optionsQuery.inviteType,
						url:`${domainUrl}?url=/pages/send/lookFor/lookHomepage/lookHomepage?id=${this.optionsQuery.id}&userId=${userId}`
					}
				}else if(this.optionsQuery.inviteType==7){
					//校园推广者
					param={
						id:'7',
						inviteType:this.optionsQuery.inviteType,
						url:'/pages/index/index',
						placardUrl:this.optionsQuery.posterUrl
					}
				}
				inviteShare(param).then((res)=>{
					uni.hideLoading()
					if(res.code==100000){
						console.error("邀请参数",res,param)
						if(param.inviteType==7){
							this.shareImg=res.data.zcPlacard
						}else{
							this.shareImg=res.data
						}
					}
				}).catch((err)=>{uni.hideLoading()})
			},
			shareClick(item){
				if(item.type=='camera'){
					// 小程序判断有没有授权保存图片
					// #ifdef MP
					uni.authorize({
					    scope: 'scope.writePhotosAlbum',
					    success:(succ)=> {
							console.error("succ",succ)
					        this.savePic()
					    },
						fail(err) {
							console.error('获取授权1111',err)
							uni.showModal({
								title: '提示',
								content: '检测到您拒绝了相册的授权，是否前去设置打开？',
								success: (res)=> {
									if (res.confirm) {
										uni.openSetting({
											success(res) {
												console.log(res.authSetting)
											}
										});
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
							// if(err.errMsg.indexOf('authorize:fail:access deny')!=-1){
							// 	console.error('获取授权',err)
								
							// }
							
						}
					})
					// #endif
					// #ifdef APP-PLUS
					let permisionStr=''
					if(uni.getStorageSync('systemInfo').platform=='ios'){
						permisionStr='photoLibrary'
					}else{
						permisionStr='android.permission.WRITE_EXTERNAL_STORAGE'
					}
					requestAndroidPermission(permisionStr).then((status)=>{
						if(status!==1 || status===false){
							return
						}
						this.savePic()
					})
					// #endif
					
				}else{
					uni.share({
						provider: "weixin",
						scene: item.type,
						type: 2,
						imageUrl: this.shareImg,
						success: function (res) {
							console.log("success:" + JSON.stringify(res));
						},
						fail: function (err) {
							console.log("fail:" + JSON.stringify(err));
						}
					});
				}
			},
			savePic(){
				// 保存图片
				uni.getImageInfo({
					src: this.shareImg,
					success:(image)=> {
						console.log(image);
						console.log(image.height);
						uni.saveImageToPhotosAlbum({
							filePath: image.path,
							success: ()=> {
								console.log('save success');
								uni.showToast({
									title:'保存成功',
									icon:'none'
								})
							},
							fail(err) {
								uni.showToast({
									title:'保存失败,请检查是否授予相册权限',
									icon:'none'
								})
							}
						});
					},
					fail() {
						
					}
				});
			},
			previewClick(){
				console.log('点击')
				previewImg([this.shareImg])
			}
		}
	}
</script>

<style lang="scss" scoped>
.shareLayer{
	position: fixed;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	background: rgba(0, 0, 0, .6);
}
.shareCon{
	width: 100%;
	text-align: center;
	/* #ifdef H5 */
	padding:150rpx 0 0;
	box-sizing: border-box;
	/* #endif */
	/* #ifndef H5 */
	padding:50rpx 0 0;
	box-sizing: border-box;
	/* #endif */
}
.poster{
	width: 70%;
	display: block;
	margin: 0 auto 30rpx;
}
</style>
